var article = require('../app/controllers/article')
  , adminIndex = require('../app/controllers/admin/index')
  , adminLogin = require('../app/controllers/admin/login')
  , adminArticle = require('../app/controllers/admin/article')
  , env = process.env.NODE_ENV || 'development'
  , config = require('../config/config')[env]
  fs = require('fs');

module.exports = function(app) {
  //routes
  app.get('/', article.index)
  app.post('/article/add', article.addArticle);
  app.get('/article/add', article.addArticleShow);
  app.get('/article/:id', article.load);
  app.get('/article/edit/:id', article.edit);
  app.post('/article/update',  article.update);
  app.get('/article/list', article.articleList);
  app.get('/article/list/:page.html', article.articleList);
  app.get('/article/delete/:id', article.deleteArticle)
  //文件上传
  app.post('/upload.js', function(req, res){
    var savePath = config.uploadPath
      , date = new Date()
      , ymd = ''+date.getFullYear()+(date.getMonth()+1)+date.getDate()+'/';
    if(!fs.exists(savePath)) {
      fs.mkdir(savePath, function(){});
    }
    savePath += ymd;
    if (!fs.exists(savePath)) {
      fs.mkdir(savePath, function(){});
    }
    fs.readFile(req.files.imgFile.path, function(err, data) {
      fs.writeFile(savePath+req.files.imgFile.originalFilename, data, function(err){
        if(err) {
          console.log(err);
        } else {
          res.json({error : 0, url : '/upload/'+ymd+req.files.imgFile.originalFilename})
        }
      })
    });
  });
  //文件空间
  app.get('/filemanager.js', function(req, res) {
    var info = {}
      , isDir = function(dir) {
        fs.readdir(dir, function(err, files){
          if (err) {
            console.log(err);
            return false;
          } else {
            return true;
          }
        })
      , isPhoto = function(file) {
          return /\.jpg|png|gif|jpeg$/i.test(file);
        };
      };
    info.file_list = [];
    info.current_url = '/upload/';
    info.moveup_dir_path = '';
    fs.readdir(config.uploadPath, function(err, files) {
      files.forEach(function(fileName){
        var curInfo = {};
        curInfo.is_dir = false; //isDir(config.uploadPath+fileName);
        curInfo.is_photo = /\.jpg|png|gif|jpeg$/i.test(fileName);
        curInfo.filename = fileName;
        console.log(curInfo);
        info.file_list.push(curInfo);
      });
      var dirInfo = {
        "moveup_dir_path": "",
        "current_dir_path": "20140310/",
        "current_url": "/Extend/Util/Kindeditor/php/../../../../Uploadfiles/image/20140310/",
        "total_count": 45,
        "file_list": [
          {
            "is_dir": false,
            "has_file": false,
            "filesize": 60023,
            "dir_path": "",
            "is_photo": true,
            "filetype": "png",
            "filename": "20140310112455_89458.png",
            "datetime": "2014-03-10 11:24:55"
          },
          {
            "is_dir": true,
            "has_file": true,
            "filesize": 0,
            "is_photo": false,
            "filetype": "",
            "filename": "20140310",
            "datetime": "2014-03-10 16:39:02"
          },
          {
            "is_dir": false,
            "has_file": false,
            "filesize": 55771,
            "dir_path": "",
            "is_photo": true,
            "filetype": "png",
            "filename": "20140310163902_29283.png",
            "datetime": "2014-03-10 16:39:02"
          }
        ]
      };
      res.json(dirInfo);
    });
  })
  //后台部分
  app.get('/admin', adminIndex.index);
  app.get('/admin/article/list', adminArticle.list);
  app.get('/admin/article/add', adminArticle.add);
  app.get('/admin/article/edit/:id', adminArticle.edit);
  app.get('/admin/article/delete/:id', adminArticle.delete);
  app.post('/admin/article/update', adminArticle.update);
  //登陆
  app.get('/admin/login_view', adminLogin.loginView);
  app.post('/admin/check_login', adminLogin.checkLogin);
  app.get('/admin/success', adminLogin.success);
}